//
//  ScreeningView.m
//  DaMaoMall
//
//  Created by HXHL-wally on 16/7/27.
//  Copyright © 2016年 daMao-wally. All rights reserved.
//

#import "ScreeningView.h"
#import "ScreeningTableViewCell.h"


@interface ScreeningView ()<UITableViewDelegate,UITableViewDataSource,ScreeningCellDelegate>

/** 重置按钮 */
@property (strong, nonatomic) UIButton * resetBut;
/** 确定按钮 */
@property (strong, nonatomic) UIButton * determineBut;
/** 展示tableView */
@property (strong, nonatomic) UITableView *tableV;
/** 选项标题数组 */
@property (strong, nonatomic) NSMutableArray *headerTitArr;
/** 选项数据数组 */
@property (strong, nonatomic) NSMutableArray *dataArr;
/** 是否展开状态数组 */
@property (strong, nonatomic) NSMutableArray *shrinkArr;
/** 是否选中状态字典 */
@property (strong, nonatomic) NSMutableDictionary *selectedDict;

@end

@implementation ScreeningView

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.headerTitArr = [NSMutableArray arrayWithObjects:
                         @"交易筛选"
                         ,@"订单编号:"
                         ,@"支付类型:"
                         ,@"类型:"
                         ,@"创建时间:"
                         , nil];
//    self.dataArr = @[@[],@[],@[@"全部",@"应收款",@"已付款",@"应收款",@"已收款"],@[@"全部",@"等待卖家确认",@"等待买家付款",@"卖家已确认"],@[]];
    self.dataArr = [NSMutableArray arrayWithObjects:@"全部",@"应收款",@"已付款",@"应收款", nil];
    self.shrinkArr = [NSMutableArray array];
    self.selectedDict = [NSMutableDictionary dictionary];
    for (int i = 0; i < self.headerTitArr.count; i++) {
        [self.shrinkArr addObject:@"NO"];
        NSMutableArray *selectedArr = [NSMutableArray array];
        for (int i = 0; i < self.dataArr.count; i++) {
            [selectedArr addObject:@"NO"];
        }
        [self.selectedDict setObject:selectedArr forKey:[NSString stringWithFormat:@"%d", i]];
    }
    
    [self.resetBut addTarget:self action:@selector(resetButClick) forControlEvents:UIControlEventTouchUpInside];
    [self.determineBut addTarget:self action:@selector(determineButClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.tableV reloadData];
}
#pragma mark - 按钮点击事件
//重置
- (void)resetButClick{
    [self.shrinkArr removeAllObjects];
    [self.selectedDict removeAllObjects];
    for (int i = 0; i < self.headerTitArr.count; i++) {
        [self.shrinkArr addObject:@"NO"];
        NSMutableArray *selectedArr = [NSMutableArray array];
        for (int i = 0; i < self.dataArr.count; i++) {
            [selectedArr addObject:@"NO"];
        }
        [self.selectedDict setObject:selectedArr forKey:[NSString stringWithFormat:@"%d", i]];
    }
    [self.tableV reloadData];
}
//确定
- (void)determineButClick{
    
    NSMutableArray *strArr = [NSMutableArray array];//
 
    
    for (int i = 1; i < self.headerTitArr.count; i++) {
        
        NSString *sectionStr = [NSString stringWithFormat:@"%d", i];
        NSMutableArray *sectionArr = self.selectedDict[sectionStr];
        
        NSMutableArray *lineArr = [NSMutableArray array];
        
        for (int i = 0; i < sectionArr.count; i++) {
            if ([sectionArr[i] isEqualToString:@"YES"]) {
                [lineArr addObject:[NSString stringWithFormat:@"%@", self.dataArr[i]]];
                
                [strArr addObject:self.dataArr[i]];
            }
        }
        //        if (lineArr.count > 0) {
        //
        //            [dict setObject:lineArr forKey:self.headerTitArr[i]];
        //
        //        }
    }
    NSLog(@"筛选条件 : \n%@", [self dictionaryToJson:strArr]);
    //    NSLog(@"筛选条件 : \n%@", strArr);
}
/** 字典转json字符串 */
- (NSString*)dictionaryToJson:(id)data
{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
- (void)shrinkButClick:(UIButton *)button
{
    
    if ([self.shrinkArr[button.tag] isEqualToString:@"NO"]) {
        NSLog(@"点击了第%ld块,展开", button.tag);
        [self.shrinkArr replaceObjectAtIndex:button.tag withObject:@"YES"];
    }else {
        NSLog(@"点击了第%ld块,收缩", button.tag);
        [self.shrinkArr replaceObjectAtIndex:button.tag withObject:@"NO"];
    }
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:button.tag];
    [self.tableV reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - BrandCellDelegate
/** 取得选中选项的值，改变选项状态，刷新列表 */
- (void)selectedValueChangeBlock:(NSInteger)section key:(NSInteger)index value:(NSString *)value
{
    
    NSString *sectionStr = [NSString stringWithFormat:@"%ld", section];
    NSMutableArray *arr = self.selectedDict[sectionStr];
    [arr replaceObjectAtIndex:index withObject:value];
    [self.selectedDict setObject:arr forKey:sectionStr];
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:section];
    [self.tableV reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - tableView UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.headerTitArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section != 0) {
        ScreeningTableViewCell *cell = [[ScreeningTableViewCell alloc]init];
        cell.isShrinkage = self.shrinkArr[indexPath.section];
        cell.attributeArr = self.dataArr;
        return cell.height;
    }
    return 15;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreeningOffset, 40)];
    if (section == 0) {
        UILabel *titLab = [[UILabel alloc]init];
        titLab.text = self.headerTitArr[section];
        titLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0];
        CGSize titSize = [titLab.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0]}];
        titLab.frame = CGRectMake(5, 5, titSize.width, 40);
        [myView addSubview:titLab];
    }else{
        UILabel *titLab = [[UILabel alloc]init];
        titLab.text = self.headerTitArr[section];
        titLab.font = [UIFont systemFontOfSize:15.0];
        CGSize titSize = [titLab.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0]}];
        titLab.frame = CGRectMake(5, 0, titSize.width, 40);
        [myView addSubview:titLab];
    }
    if (section == 2 || section == 3) {
        UIButton *shrinkBut = [[UIButton alloc]initWithFrame:CGRectMake(ScreeningOffset - 40, 0, 40, 40)];
        shrinkBut.tag = section;
        [shrinkBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        if ([self.shrinkArr[section] isEqualToString:@"NO"]) {
            [shrinkBut setTitle:@"展开" forState:UIControlStateNormal];
            shrinkBut.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        }else {
            [shrinkBut setTitle:@"收起" forState:UIControlStateNormal];
            shrinkBut.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        }
        [shrinkBut addTarget:self action:@selector(shrinkButClick:) forControlEvents:UIControlEventTouchUpInside];
        [myView addSubview:shrinkBut];
    }
    
    return myView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2 || indexPath.section == 3) {
        ScreeningTableViewCell *cell = [ScreeningTableViewCell cellWithTableView:tableView dataArr:self.dataArr indexPath:indexPath];
        /** 取消cell点击状态 */
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.tag = indexPath.section;
        cell.delegate = self;
        
        NSString *sectionStr = [NSString stringWithFormat:@"%ld", indexPath.section];
        cell.selectedArr = self.selectedDict[sectionStr];
        
        cell.isShrinkage = self.shrinkArr[indexPath.section];
        
        cell.attributeArr = self.dataArr;
        
        return cell;
    }else{
        static NSString *identifier = @"Cell";
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        return cell;
    }
    
}
#pragma mark - 懒加载
-(UITableView *)tableV
{
    if (!_tableV) {
        _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.width, [UIScreen mainScreen].bounds.size.height - 49) style:UITableViewStyleGrouped];
        _tableV.backgroundColor = [UIColor whiteColor];
        /** 隐藏cell分割线 */
        [_tableV setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        _tableV.delegate = self;
        _tableV.dataSource = self;
        [self.view addSubview:_tableV];
    }
    return _tableV;
}

- (UIButton *)resetBut//重置
{
    if (!_resetBut) {
        _resetBut = [[UIButton alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 49, self.width/2, 49)];
        [_resetBut setBackgroundColor:[UIColor whiteColor]];
        [_resetBut setTitle:@"重置" forState:UIControlStateNormal];
        [_resetBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_resetBut setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [self.view addSubview:_resetBut];
    }
    return _resetBut;
}
- (UIButton *)determineBut//确定
{
    if (!_determineBut) {
        _determineBut = [[UIButton alloc]initWithFrame:CGRectMake(self.width/2, [UIScreen mainScreen].bounds.size.height - 49, self.width/2, 49)];
        [_determineBut setBackgroundColor:[UIColor redColor]];
        [_determineBut setTitle:@"确定" forState:UIControlStateNormal];
        [_determineBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_determineBut setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        [self.view addSubview:_determineBut];
    }
    return _determineBut;
}


@end
